/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.person;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.person.TelecomDetails;

/**
 *
 * @author Gebruiker
 */
@Stateless
public class TelecomFacade extends AbstractFacade<Telecom> implements TelecomFacadeLocal {
    @EJB
    private SupplierFacadeLocal supplierFacade;
    @EJB
    private TelecomtypeFacadeLocal telecomtypeFacade;
    
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;
    
    

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TelecomFacade() {
        super(Telecom.class);
    }
    
    @Override
    public Integer createTelecom(String value, Integer telecomTypeid, Integer supplierid){
        Telecomtype type = telecomtypeFacade.find(telecomTypeid);
        Supplier supplier = supplierFacade.find(supplierid);
        Telecom telecom = new Telecom(value, type, supplier);
        create(telecom);
        return telecom.getId();   
    }
    
    @Override
    public void removeTelecom(Integer id){
        Telecom telecom = find(id);
        remove(telecom);
    }
    
    @Override
    public void editTelecom(String value, Integer telecomTypeid, Integer id){
        Telecomtype type = telecomtypeFacade.find(telecomTypeid);
        Telecom telecom = find(id);
        telecom.setValue(value);
        telecom.setTelecomTypeid(type);
        edit(telecom);
    }
    
    @Override
    public TelecomDetails getTelecomDetails(Integer id){
        return convert(find(id));
    }
    
    public TelecomDetails convert(Telecom telecom){
        return new TelecomDetails(telecom.getId(), telecom.getValue(), telecom.getTelecomTypeid().getId());
    }
    
}
